package com.bzqn.baseframe;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;

public class BaseTabar extends LinearLayout implements ViewUnitContent {

	private int mScreenWith;
	private TabBarConfig mTabBarConfig;
	private RadioGroup mRadio;
	private int mCurrentPageIndex = 0;
	private OnTabBarChangeListener listener;
	public interface OnTabBarChangeListener{
		void onTabBarChange(int index);
	}
	public void setOnTabBarChangeListener(OnTabBarChangeListener listener){
		this.listener = listener;
	}
	public BaseTabar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public BaseTabar(Context context) {
		super(context);
		init(context);
	}

	public void setCurrentPageIndex(int index){
		mCurrentPageIndex = index;
		setCurrentPage(index);
	}
	public void resetView(Context context){
		if(mRadio != null){
			mRadio.removeAllViews();
			init(context);
		}
	}
	public BaseTabar(Context context , TabBarConfig config, OnTabBarChangeListener listener) {
		super(context);
		mTabBarConfig = config;
		this.listener = listener;
		init(context);
	}
	private void init(final Context context) {
		if (mTabBarConfig == null) {
			mTabBarConfig = new TabBarConfig();
		}
		setOrientation(LinearLayout.VERTICAL);
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(dm);
		mScreenWith = dm.widthPixels;
		setOrientation(LinearLayout.HORIZONTAL);
		mRadio = new RadioGroup(context);
		mRadio.setOrientation(LinearLayout.HORIZONTAL);
		LinearLayout.LayoutParams radioGroupLayoutParams = new LayoutParams(
				MATCH_PARENT, Utils.dip2px(mTabBarConfig.getTabBarHeight(),
						context));
		mRadio.setGravity(Gravity.CENTER);
		addView(mRadio, radioGroupLayoutParams);
		if (mTabBarConfig.getTabBarBackgroundDrawableResId() != 0) {
			mRadio.setBackgroundResource(mTabBarConfig
					.getTabBarBackgroundDrawableResId());
		} else if (mTabBarConfig.getTabBarBackgroundColor().startsWith(
				"http://")
				|| mTabBarConfig.getTabBarBackgroundColor().startsWith(
						"HTTP://")) {
			mRadio.setBackgroundColor(Color.GRAY);
			FinalBitmap.create(context).display(mRadio,
					mTabBarConfig.getTabBarBackgroundColor());
		} else {
			mRadio.setBackgroundColor(Color.parseColor(mTabBarConfig
					.getTabBarBackgroundColor()));
		}
		if (mTabBarConfig.getButtons() == null || mTabBarConfig.getButtons().size() == 0) {
			return; 
		}
		for (int i = 0; i < mTabBarConfig.getButtons().size(); i++) {
			TabButtonConfig config = mTabBarConfig.getButtons().get(i);
			config.setPageIndex(i);
			if (config.getUnCheckResId() == 0 && config.getCheckedResId() == 0) {
				throw new RuntimeException(
						"请检查TabButtonConfig中的setTabButtonImageResId是否是有效的资源ID");
			}
			LinearLayout.LayoutParams childRdioButtonParams = new LayoutParams(
					mScreenWith / mTabBarConfig.getButtons().size(),
					Utils.dip2px(config.getTabButtonHeight(), context), Utils.dip2px(config.getTabButtonHeight()-10, context));
			RadioButton childRadioButton = new RadioButton(context);
			childRadioButton.setTag(config);
			childRadioButton.setGravity(Gravity.CENTER);
			childRadioButton.setText(config.getTabButtonText());
			childRadioButton.setPadding(0, childRadioButton.getPaddingTop() + Utils.dip2px(5, context),
					childRadioButton.getPaddingRight(),
					childRadioButton.getPaddingBottom()); 
			if (config.isChecked()) {
				childRadioButton.setCompoundDrawablesWithIntrinsicBounds(
						null,
						context.getResources().getDrawable(
								config.getCheckedResId()), null, null);
				childRadioButton.setTextColor(Color.parseColor(config
						.getTabButtonCheckedTextColor()));
			} else {
				childRadioButton.setCompoundDrawablesWithIntrinsicBounds(
						null,
						context.getResources().getDrawable(
								config.getUnCheckResId()), null, null);
				childRadioButton.setTextColor(Color.parseColor(config
						.getTabButtonUnCheckedTextColor()));
			}
			childRadioButton.setButtonDrawable(android.R.color.transparent);
			childRadioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP,
					config.getTabButtonTextSize());
			childRadioButton.setCompoundDrawablePadding(config.getPadding());
			childRadioButton.setId(i * 100 + 1008);
			mRadio.addView(childRadioButton, childRdioButtonParams);
		}
		mRadio.setOnCheckedChangeListener(new CheckedListener(context));
		setCurrentPage(mCurrentPageIndex);
	}

	private void setCurrentPage(int index){
		if(mCurrentPageIndex <= mTabBarConfig.getButtons().size()){
			RadioButton button = (RadioButton) mRadio.getChildAt(mCurrentPageIndex);
			if(button != null){
				button.setChecked(true);
			}
		}
	}
	/**
	 * 添加到RelativeLayout容器上
	 * 
	 * @param parent
	 */
	public void addToRelativeLayoutParent(RelativeLayout parent, Context context) {
		RelativeLayout.LayoutParams baseTabLayoutParams = new RelativeLayout.LayoutParams(
				MATCH_PARENT, Utils.dip2px(mTabBarConfig.getTabBarHeight(), context));
		baseTabLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		parent.addView(this, baseTabLayoutParams);
	}

	public TabBarConfig getmTabBarConfig() {
		return mTabBarConfig;
	}

	public void setmTabBarConfig(TabBarConfig mTabBarConfig) {
		this.mTabBarConfig = mTabBarConfig;
	}

	private class CheckedListener implements OnCheckedChangeListener {

		private Context context;

		public CheckedListener(Context context) {
			this.context = context;
		}

		@Override
		public void onCheckedChanged(RadioGroup group, int id) {
			int count = group.getChildCount();
			for (int i = 0; i < count; i++) {
				RadioButton button = (RadioButton) group.getChildAt(i);
				if (button.getId() != id) {
					button.setCompoundDrawablesWithIntrinsicBounds(
							null,
							context.getResources().getDrawable(
									mTabBarConfig.getButtons().get(i)
											.getUnCheckResId()), null, null);
					button.setTextColor(Color.parseColor(mTabBarConfig
							.getButtons().get(i)
							.getTabButtonUnCheckedTextColor()));
				} else {
					button.setCompoundDrawablesWithIntrinsicBounds(
							null,
							context.getResources().getDrawable(
									mTabBarConfig.getButtons().get(i)
											.getCheckedResId()), null, null);
					button.setTextColor(Color
							.parseColor(mTabBarConfig.getButtons().get(i)
									.getTabButtonCheckedTextColor()));
					//如果选中输入这个ID
					if(listener != null){
						listener.onTabBarChange(mTabBarConfig.getButtons().get(i).getPageIndex());
					}
				}

			}
		}

	}
}
